"""
对request的二次封装
1. 使用requests模块的session机制来发送请求
2. 将base_url地址的获取放在该文件当中进行处理，测试用例当中调用接口只需要传入接口地址即可
"""
import requests
from lib.config import CONFIG
from lib.logger import Logger

class HTTPClient(object):

    def __init__(self):
        self.s = requests.session()

    def __del__(self):
        self.s.close()

    def request(self, path, **kwargs):
        base_url = CONFIG["api_test"]["base_url"]
        r = self.s.request(url=base_url+path, **kwargs)
        logger = Logger()
        logger.debug("== 请求:")
        logger.debug(r.request.url)
        logger.debug(r.request.method)
        logger.debug(r.request.headers)
        logger.debug(r.request.body)
        logger.debug("== 响应:")
        logger.debug(r.status_code)
        logger.debug(r.headers)
        logger.debug(r.text)
        return r

    def update(self, headers):
        self.s.headers.update(headers)